﻿using System.Windows;
using App;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace Regression
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var model = new PlotModel
            {
                PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 0),
                PlotMargins = new OxyThickness(10, 10, 10, 10)
            };
            
            var arrayX = new[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d };
            var arrayY = new[] { 0.5, 2.5, 2.0, 4.0, 3.5, 6.0, 5.5 };

            var lr = new LinearRegression(arrayX, arrayY);
            
            var a = lr.Result["a"];
            var b = lr.Result["b"];

            var lineSeries = new LineSeries();

            for(var i = 0; i<=arrayY.Length;i++)
                lineSeries.Points.Add(new DataPoint(i,(a*i+b)));

            var pointSeries = new LineSeries
            {
                MarkerType = MarkerType.Square,
                LineStyle = LineStyle.None
            };

            for (var i = 0; i < arrayX.Length; i++)
                pointSeries.Points.Add(new DataPoint(arrayX[i], arrayY[i]));

            var linearAxis1 = new LinearAxis
            {
                Maximum = 10,
                Minimum = -10,
                PositionAtZeroCrossing = true,
                TickStyle = TickStyle.Crossing
            };

            var linearAxis2 = new LinearAxis
            {
                Maximum = 10,
                Minimum = -10,
                Position = AxisPosition.Bottom,
                PositionAtZeroCrossing = true,
                TickStyle = TickStyle.Crossing
            };

            model.Axes.Add(linearAxis1);
            model.Axes.Add(linearAxis2);
            model.Series.Add(pointSeries);
            model.Series.Add(lineSeries);

            model.Title = $"f(x) = {lr.Result["a"]}x + {lr.Result["b"]}";

            Chart.Model = model;
        }
    }
}